package cn.mothin.action.setup;

import org.springframework.beans.factory.annotation.Autowired;

import cn.mothin.frame.bean.ActionResult;
import cn.mothin.frame.core.base.BaseAction;
import cn.mothin.frame.help.Password;
import cn.mothin.service.setup.RegisterSuperService;
import cn.mothin.vo.setup.RegisterSuperForm;

@SuppressWarnings("serial")
public class RegisterSuperAction extends BaseAction {
	private RegisterSuperForm registerSuperForm = new RegisterSuperForm();
	@Autowired
	private RegisterSuperService registerSuperService;

	@Override
	public Object getModel() {
		return registerSuperForm;
	}
	
	/**
	 * 判断是否有超级用户
	 * 
	 * @return true：不存在，false：已存在记录
	 */
	public String execute() {
		boolean result = this.registerSuperService.querySuperUserExist();
		if (result) {
			return "exist";
		}
		registerSuperForm.setMd5Time(Password.MD5TIME);
		return "add";
	}

	/**
	 * 添加超级用户
	 * 
	 * @return 0：成功；1：已存在一条记录，2：未知错误
	 */
	public String saveForAddSuperUser() {
		int res = registerSuperService.addSuperUser(registerSuperForm);
		ActionResult result=new ActionResult();
		if(res == 0) {
			result.setOk(true);
		}
		if (res == 1) {
			result.setOk(false);
			result.setMessage("超级用户已存在");
		}
		if (res == 2) {
			result.setOk(false);
		}
		return renderJson(result.toJson());
	}
}
